using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace Destra.Tools.DACGenerator
{
	public class SqlFile
	{
		public static void Parse(string filePath, out string configXml, out string sql)
		{
			configXml = string.Empty;
			sql = string.Empty;
			string contents = null;

			using (StreamReader reader = new StreamReader(filePath))
			{
				contents = reader.ReadToEnd();
			}

			int commentBlockLength = 0;

			Regex regex = new Regex(@"/\*(?<config>.*)\*/", RegexOptions.Singleline);
			Match match = regex.Match(contents);
			if (match.Success)
			{
				configXml = match.Groups["config"].Value;

				commentBlockLength = "/*".Length + configXml.Length + "*/".Length;
			}

			if (commentBlockLength < contents.Length)
			{
				sql = contents.Substring(commentBlockLength);
			}

			configXml = configXml.Trim();
		}
	}
}
